// pages/order/evaluate/evaluate.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		type: '',
		goodsList: [],
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.data.orderId = options.id;
		const type = this.data.type = options.type || '';
		if ('course' === type) {
			this.loadCourseDetail();
		} else {
			this.loadDetail();
		}
	},

	// 加载商品列表
	loadDetail: function() {
		return wx.http.get('api/user.comment/order', {
			order_id: this.data.orderId
		}).then((res) => {
			const goodsList = res.data.data.goodsList;
			this.setData({
				goodsList: goodsList,
			});
			return goodsList;
		}, (err) => {
			console.warn("api/user.comment/order", err)
		});
	},

	// 加载课程列表
	loadCourseDetail: function() {
		return wx.http.get('api/curriculum.comment/order', {
			order_id: this.data.orderId
		}).then((res) => {
			const goodsList = res.data.data.goodsList;
			this.setData({
				goodsList: goodsList,
			});
			return goodsList;
		}, (err) => {
			console.warn("api/user.comment/order", err)
		});
	},

	// 提交
	onSubmit: function(e) {
		const values = e.detail.value;
		const result = [];
		for (const key in values) {
			const value = values[key];
			if (value && (value.uploaded.length || value.content.length)) {
				if (value.uploaded.length) {
					value.uploaded = value.uploaded.filter(it => it)
				}
				result.push(value);
			}
		}

		const url = 'course' === this.data.type ? 'api/curriculum.comment/order' : 'api/user.comment/order';
		return wx.http.post(url, {
			order_id: this.data.orderId,
			curriculum_order_id: this.data.orderId,
			body: JSON.stringify(result),
			// body: result
		}).then((res) => {
			wx.showModal({
				content: '提交成功!',
				showCancel: false,
				success(res) {
                    wx.navigateBack({
                        delta: 1
                    })
				}
			});
			// wx.showToast({
			// 	title: '提交成功',
			// 	icon: 'success',
			// 	duration: 2000
			// });
			wx.emitter.emit('order.evaluate', this.data.orderId);
		});
	},
});
